package com.ymsoft.myerp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ymsoft.myerp.common.BaseController;
import com.ymsoft.myerp.common.DTO;
import com.ymsoft.myerp.entity.Permission;
import com.ymsoft.myerp.service.IMenuService;
import com.ymsoft.myerp.service.IPermissionService;
import com.ymsoft.myerp.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ymsoft
 * @since 2020-04-12
 */
@Controller
@RequestMapping("/myerp/permission")
public class PermissionController extends BaseController {

    @Autowired
    private IPermissionService iPermissionService;

    @Autowired
    private IMenuService iMenuService;

    //分页查询
    @RequestMapping("/list")
    @ResponseBody
    public DTO<?> list(@Valid Permission permission, int page, int limit) {
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(permission.getName())) {
            queryWrapper.like("name", permission.getName());
        }
        if (StringUtils.isNotBlank(permission.getUrl())) {
            queryWrapper.like("url", permission.getUrl());
        }
        if (permission.getPid() != null && !"".equals(permission.getPid())) {
            queryWrapper.eq("pid", permission.getPid());
        }
        Page<Permission> Page = new Page<>();
        IPage<Permission> ipage = iPermissionService.page(Page, queryWrapper);
        return DTO.retSuccess(ipage.getRecords(), (int) ipage.getTotal());
    }

    //查询全部权限及全部的菜单
    @RequestMapping("/listAll")
    @ResponseBody
    public DTO<?> listAll() {
        return DTO.retSuccess(iPermissionService.list(), iPermissionService.list().size());
    }

    @PostMapping("/delete")
    @ResponseBody
    public DTO<?> delete(@Valid Permission permission) {
        boolean isOk = iPermissionService.removeById(permission.getId());
        if (isOk) {
            return DTO.retSuccess();
        } else {
            return DTO.retFailed();
        }
    }

    @PostMapping("/update")
    @ResponseBody
    public DTO<?> updata(@Valid Permission permission) {
        boolean isOk = iPermissionService.updateById(permission);
        if (isOk) {
            return DTO.retSuccess();
        } else {
            return DTO.retFailed();
        }
    }

    @PostMapping("/add")
    @ResponseBody
    public DTO<?> add(@Valid Permission permission) {
        boolean isOk = iPermissionService.save(permission);
        if (isOk) {
            return DTO.retSuccess();
        } else {
            return DTO.retFailed();
        }
    }

}
